<?php
// +----------------------------------------------------------------------
// | Author: huangzhen_20210406
// +----------------------------------------------------------------------

namespace app\common\model;
use think\facade\Db;

class CdApiLog extends Common
{
    //---------------------------------------------后端---------------------------------------------------------------
    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableDataAdmin($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        if(isset($post['user_name']) && !empty($post['user_name'])){
            $where[] = ['username', 'like', '%'.$post['user_name'].'%'];
        }
        if(isset($post['card_num']) && !empty($post['card_num'])){
            $where[] = ['card_num', 'like', '%'.trim($post['card_num']).'%'];
        }
        if(isset($post['request']) && !empty($post['request'])){
            $where[] = ['request', 'like', '%'.trim($post['request']).'%'];
        }
        if(isset($post['header']) && !empty($post['header'])){
            $where[] = ['header', 'like', '%'.trim($post['header']).'%'];
        }
        if(isset($post['response']) && !empty($post['response'])){
            $where[] = ['response', 'like', '%'.$post['response'].'%'];
        }
        if(isset($post['datetime']) && !empty($post['datetime'])){
            $time_list = explode('_' , $post['datetime']);
            $start_time = strtotime($time_list[0] . " 00:00:00");
            $end_time   = strtotime($time_list[1] . " 23:59:59");
            $where[] = ['al.ctime', 'between', "{$start_time},{$end_time}"];
        }
        $list = Db::name("cd_api_log")->alias('al')
            ->leftJoin('user u' , 'u.id=al.user_id')
            ->field('username,al.*')
            ->order('al.ctime desc')
            ->where($where)
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();

        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }
}